﻿using System;

using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace App_csharp
{
    public partial class Form1 : Form
    {
        private CDD dd;
        private bool kaiGuan = false;
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {

            reg_hotkey();                            // 注册热键

            dd = new CDD();

            //加载DLL
            string dllfile = ReadDataFromReg();

            //LoadDllFile(dllfile);
            //return;

            //或手动定位
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "DD入口文件|*.DLL";

            //if (ofd.ShowDialog() != DialogResult.OK)
            //{
            //    return;
            //}

            ofd.FileName = Environment.CurrentDirectory + @"\DD94687.64.dll";
            LoadDllFile(ofd.FileName);
        }

        /// <summary>
        /// 关闭程序卸载热键
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            unreg_hotkey();
            ////精确进程名  用GetProcessesByName
            //foreach (Process p in Process.GetProcessesByName(strProcName))
            //{
            //    if (!p.CloseMainWindow())
            //    {
            //        p.Kill();
            //    }
            //}
        }
        




      

        /// <summary>
        /// 加载DLL
        /// </summary>
        /// <param name="dllfile"></param>
        private void LoadDllFile(string dllfile)
        {


            System.IO.FileInfo fi = new System.IO.FileInfo(dllfile);
            if (!fi.Exists)
            {
                MessageBox.Show("文件不存在");
                return;
            }

            int ret = dd.Load(dllfile);
            if (ret == -2) { MessageBox.Show("装载库时发生错误"); return; }
            if (ret == -1) { MessageBox.Show("取函数地址时发生错误"); return; }

            return;
        }

        /// <summary>
        /// 加载DLL
        /// </summary>
        /// <returns></returns>
        private string ReadDataFromReg()
        {
            Microsoft.Win32.RegistryKey key;
            key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\DD XOFT\\", false);
            if (key != null)
            {
                foreach (string vname in key.GetValueNames())
                {
                    if ("path" == vname.ToLower())
                    {
                        return key.GetValue(vname, "").ToString();
                    }
                }
            }
            return "";
        }

 #region "热键设置相关代码"
        [DllImport("user32.dll")]
        public static extern bool RegisterHotKey(
         IntPtr hWnd,
         int id,                            // 热键标识
         KeyModifiers modkey,  //  修改键
         Keys vk                         //  虚键码
        );
        [DllImport("user32.dll")]
        public static extern bool UnregisterHotKey(
         IntPtr hWnd,              // 窗口句柄 
         int id                          // 热键标识 
        );

        /// <summary>
        /// 注册热键
        /// </summary>
        void reg_hotkey()
        {
            RegisterHotKey(this.Handle, 70, 0, Keys.Oemcomma); //逗号
        }

        /// <summary>
        /// 卸载热键
        /// </summary>
        void unreg_hotkey()
        {
            UnregisterHotKey(this.Handle, 70);                    
        }

        /// <summary>
        /// 持续监听
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref Message m)
        {
            const int WM_HOTKEY = 0x0312;                        //0x0312表示用户热键
            switch (m.Msg)
            {
                case WM_HOTKEY:
                    ProcessHotkey(m);                                      //调用ProcessHotkey()函数
                    break;
            }
            base.WndProc(ref m);
        }


        /// <summary>
        /// 监听热键
        /// </summary>
        private void ProcessHotkey(Message msg)              //按下设定的键时调用该函数
        {
            switch (msg.WParam.ToInt32())
            {
                // 逗号开关timer
                case 70:
                    if (timer2.Enabled == true)
                    {
                        timer2.Enabled = false;
                    }
                    else
                    {
                        timer2.Enabled = true;
                    }
                    break;
            }
        }


        #endregion

        /// <summary>
        /// 当计时器timer2开启时，无限按E和TAB
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer2_Tick(object sender, EventArgs e)
        {
            dd.key(303, 1);
            System.Threading.Thread.Sleep(5);
            dd.key(303, 2);
            System.Threading.Thread.Sleep(5);
            dd.key(300, 1);
            System.Threading.Thread.Sleep(5);
            dd.key(300, 2);
        }

        /// <summary>
        /// 程序关闭注销热键
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            unreg_hotkey();
        }
    }
}

